package com.jweb.phone.suport.inter;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.jweb.core.handle.ActionException;
import com.jweb.core.inter.HandleInterceptor;
import com.jweb.log.Logger;
import com.jweb.util.HttpServletRequestUtil;

/**
 * 请求拦截
 * 
 * @author Yuanqy
 */

public class JActionInter extends HandleInterceptor {
	private Logger log = Logger.getLogger(getClass());
	private ThreadLocal<String> curURL = new ThreadLocal<String>();
	private ThreadLocal<Long> curTime = new ThreadLocal<Long>();

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response) throws ActionException, Exception {
		curTime.set(System.currentTimeMillis());
		String curURL = request.getRequestURI();
		log.info("==>来自["+HttpServletRequestUtil.getIp(request)+"]来了一个新请求:" + curURL);
		log.info("==>参数：" + JSON.toJSONString(HttpServletRequestUtil.getParameterMap(request)));
		return true;
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex) throws ActionException {
		request.setAttribute("nowTime", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		log.info("==>请求{}共耗时：{}ms===============", curURL.get(), System.currentTimeMillis() - curTime.get());
	}
}
